// BodyCheckPage.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "BodyCheckPage.h"
#include "../Common/CommonFun.h"
#include "PatMan.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBodyCheckPage dialog


CBodyCheckPage::CBodyCheckPage(CWnd* pParent /*=NULL*/)
	: CPageDialog(CBodyCheckPage::IDD, pParent), m_pageMiniMental(pParent)
{
	//{{AFX_DATA_INIT(CBodyCheckPage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CBodyCheckPage::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBodyCheckPage)
	DDX_Control(pDX, IDB_BODYCHECK_DRAFTSAVE, m_btnDraftSave);
	DDX_Control(pDX, IDB_BODYCHECK_SAVE, m_btnSave);
	DDX_Control(pDX, IDC_TAB_BODYCHECK, m_tabBodyCheck);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBodyCheckPage, CDialog)
	//{{AFX_MSG_MAP(CBodyCheckPage)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_BODYCHECK, OnSelchangeTabBodycheck)
	ON_BN_CLICKED(IDB_BODYCHECK_DRAFTSAVE, OnBodycheckDraftSave)
	ON_BN_CLICKED(IDB_BODYCHECK_SAVE, OnBodycheckSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBodyCheckPage message handlers

BOOL CBodyCheckPage::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_pageMiniMental.Create(	CBCMiniMentalPage::IDD,				this,	0);
	m_pageTinettiBal.Create(	CBCTinettiBalPage::IDD,				this,	1);
	m_pageBergScore.Create(		CBCBergScorePage::IDD,				this,	2);
	m_pageFuglMeyerUp.Create(	CBCFuglMeyerUpPage::IDD,			this,	3);
	m_pageFuglMeyerLow.Create(	CBCFuglMeyerLowPage::IDD,			this,	4);
	m_pageHoldenWalk.Create(	CBCHoldenWalkPage::IDD,				this,	5);
	m_pageFuglMeyerBal.Create(	CBCFuglMeyerBalPage::IDD,			this,	6);
	m_pageReviseAshworth.Create(CBCReviseAshworthPage::IDD,			this,	7);
	m_pageBrunnstrom.Create(	CBCBrunnstromPage::IDD,				this,	8);
	m_pageDoctorWrite.Create(	CBCDoctorWritePage::IDD,			this,	9);
	
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 0, GetResString(IDS_BCTAB_MINI_MENTAL),				0, (LPARAM)&m_pageMiniMental);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 1, GetResString(IDS_BCTAB_TINETTI_BAL),				0, (LPARAM)&m_pageTinettiBal);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 2, GetResString(IDS_BCTAB_BERG_SCORE),				0, (LPARAM)&m_pageBergScore);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 3, GetResString(IDS_BCTAB_FUGL_MEYER_UPPER),		0, (LPARAM)&m_pageFuglMeyerUp);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 4, GetResString(IDS_BCTAB_FUGL_MEYER_LOWER),		0, (LPARAM)&m_pageFuglMeyerLow);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 5, GetResString(IDS_BCTAB_HOLDEN_WALK),				0, (LPARAM)&m_pageHoldenWalk);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 6, GetResString(IDS_BCTAB_FUGL_MEYER_LOWER_BAL),	0, (LPARAM)&m_pageFuglMeyerBal);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 7, GetResString(IDS_BCTAB_REVISE_ASHWORTH),			0, (LPARAM)&m_pageReviseAshworth);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 8, GetResString(IDS_BCTAB_BRUNNSTROM),				0, (LPARAM)&m_pageBrunnstrom);
	m_tabBodyCheck.InsertItem(TCIF_TEXT|TCIF_PARAM, 9, GetResString(IDS_BCTAB_DOCTOR_WRITE),			0, (LPARAM)&m_pageDoctorWrite);
	m_tabBodyCheck.SetItemSize(250);
	
	CRect rectTab;
	m_tabBodyCheck.GetWindowRect(&rectTab);
	CRect rectItem;
	m_tabBodyCheck.GetItemRect(0, &rectItem);

	int nPageLeft = 17;
	int nPageTop = 20 + rectItem.Height();
	int nPageWidth = rectTab.Width() - 10;
	int nPageHeight = rectTab.Height() - rectItem.Height() - 10;
	CRect rectBCPage(nPageLeft, nPageTop, nPageLeft + nPageWidth, nPageTop + nPageHeight);

	VERIFY(m_pageMiniMental.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_SHOWWINDOW));
	VERIFY(m_pageTinettiBal.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageBergScore.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageFuglMeyerUp.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageFuglMeyerLow.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageHoldenWalk.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageFuglMeyerBal.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageReviseAshworth.SetWindowPos(	&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageBrunnstrom.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));
	VERIFY(m_pageDoctorWrite.SetWindowPos(		&m_tabBodyCheck, rectBCPage.left, rectBCPage.top, rectBCPage.Width(), rectBCPage.Height(), SWP_HIDEWINDOW));

	m_tabBodyCheck.SetActiveTabColor(ACTIVE_TAB_COLOR2);

	return FALSE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CBodyCheckPage::OnSelchangeTabBodycheck(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int nCurSel = m_tabBodyCheck.GetCurSel();
	for(int i = 0; i < m_tabBodyCheck.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabBodyCheck.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
		{
			pPage->ShowWindow(i == nCurSel ? SW_SHOW : SW_HIDE);

			//  not set focus to edit box automatically

// 			if(i == nCurSel)
// 			{
//  				CWnd* pFocus = pPage->GetFocus();
// 				if(!pFocus || pFocus->GetParent() != pPage)
// 				{
// 					//  Get first tab stop control
// 					CWnd* pTab = pPage->GetNextDlgTabItem(NULL);
// 					if(pTab)
// 						pTab->SetFocus();
// 				}
// 			}
		}
	}
	
	*pResult = 0;
}

void CBodyCheckPage::OnBodycheckDraftSave() 
{
	m_bDraftSave = TRUE;

	CPatMan* pParent = (CPatMan*)GetParent();
	if(pParent)
	{
		if(!pParent->SavePatMedInfo())
			return;

		//  'saving draft' not cause to enable 'first clinic diagnose'
	}
	
	CString strMRN = GetApp()->GetCurMRN();
	if(!SaveData(strMRN))
		return;

	CString strSQL;
	strSQL.Format(_T("UPDATE PatientInfo SET DiagProgress=%d WHERE MedicalRecordNo='%s'"), TAB_INDEX_PASTHIST, strMRN);
	CGYDataSet tblInfoText;
	tblInfoText.ExecuteSQL(strSQL);

	GYMsgBox(IDS_PAT_DRAFT_SAVE_OK, MB_ICONINFORMATION);
}

void CBodyCheckPage::OnBodycheckSave() 
{
	BOOL bAllBlank = TRUE;
	for(int i = 0; i < m_tabBodyCheck.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabBodyCheck.GetItem(i, &tcItem);

		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage && !pPage->IsPageCurDayBlank())
		{
			bAllBlank = FALSE;
			break;
		}
	}

	if(bAllBlank)
	{
		if(GYMsgBox(IDS_PAT_ALLBLANK_CONTINUE, MB_YESNO|MB_ICONQUESTION) != IDYES)
			return;
	}

	if(GYMsgBox(IDS_PAT_CONFIRM_SAVE, MB_YESNO|MB_ICONQUESTION) != IDYES)
		return;

	m_bDraftSave = FALSE;
	
	CString strMRN;
	CPatMan* pParent = (CPatMan*)GetParent();
	if(!pParent)
		return;

	if(!pParent->SavePatMedInfo())
		return;

	strMRN = GetApp()->GetCurMRN();
	if(!SaveData(strMRN))
		return;

	//  now can do 1st clinic diagnose
	pParent->EnableTab(TAB_INDEX_1STDIAG, TRUE);
	pParent->SelectTab(TAB_INDEX_1STDIAG);
	
	CFirstDiagPage* p1stDiagPage =  (CFirstDiagPage*)pParent->GetTabPage(TAB_INDEX_1STDIAG);
	if(p1stDiagPage)
		p1stDiagPage->EnableInputControls(TRUE);
	
	CString strSQL;
	strSQL.Format(_T("UPDATE PatientInfo SET DiagProgress=%d WHERE MedicalRecordNo='%s'"), TAB_INDEX_BODYCHECK, strMRN);
	CGYDataSet tblInfoText;
	tblInfoText.ExecuteSQL(strSQL);
}

BOOL CBodyCheckPage::SaveData(CString strMRN)
{
	BOOL bContinue = TRUE;
	
	BOOL bAllPageBlank = TRUE;
	for(int i = 0; bContinue && i < m_tabBodyCheck.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabBodyCheck.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
		{
			int nRet = pPage->SaveData(strMRN, 0, m_bDraftSave);	//  1st time body check
			if(nRet == SaveBCE_Succ)
			{
				bContinue = TRUE;
				bAllPageBlank = FALSE;
			}
			else if(nRet == SaveBCE_PageBlank)
			{
				bContinue = TRUE;
			}
			else
			{
				bContinue = FALSE;
			}
		}
	}
	
	if(bContinue && bAllPageBlank)
		GYMsgBox(IDS_PAT_ALLPAGE_BLANK, MB_ICONINFORMATION|MB_OK);

	if(bContinue && !bAllPageBlank && !m_bDraftSave)
		EnableInputControls(FALSE);

	return bContinue;
}

BOOL CBodyCheckPage::ShowTableData(CString strMRN)
{
	CString strCheckDate;
	CString strSQL;
	strSQL.Format(_T("SELECT DiagDate FROM DiagRecord WHERE MedicalRecordNo='%s' AND DiagSN=0"), strMRN);
	CGYDataSet setDiagRec(strSQL);
	if(setDiagRec.IsEOF())
	{
		CTime tCur = CTime::GetCurrentTime();
		strCheckDate = tCur.Format(_T("%Y-%m-%d"));
	}
	else
	{
		setDiagRec.GetFieldValue(_T("DiagDate"), strCheckDate);
		strCheckDate = strCheckDate.Left(10);
	}
	setDiagRec.Close();

	BOOL bIsDraft = FALSE;
	for(int i = 0; i < m_tabBodyCheck.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabBodyCheck.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
		{
			pPage->ClearData();
			pPage->SetLastColTitile(strCheckDate, 0);
			bIsDraft = pPage->ShowTableData(strMRN, 0);
		}
	}
	
	GetDlgItem(IDB_BODYCHECK_DRAFTSAVE)->EnableWindow(bIsDraft);
	GetDlgItem(IDB_BODYCHECK_SAVE)->EnableWindow(bIsDraft);

	return TRUE;
}

BOOL CBodyCheckPage::ClearData()
{
	for(int i = 0; i < m_tabBodyCheck.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabBodyCheck.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
			pPage->ClearData();
	}

	return TRUE;
}

BOOL CBodyCheckPage::EnableInputControls(BOOL bEnable)
{
	for(int i = 0; i < m_tabBodyCheck.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabBodyCheck.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
			pPage->EnableInputControls(bEnable);
	}

	GetDlgItem(IDB_BODYCHECK_DRAFTSAVE)->EnableWindow(bEnable);
	GetDlgItem(IDB_BODYCHECK_SAVE)->EnableWindow(bEnable);

	CPageDialog::EnableInputControls(bEnable);
	return TRUE;
}

BOOL CBodyCheckPage::NewFirstBodyCheck()
{
	CTime tCur = CTime::GetCurrentTime();
	CString strToday = tCur.Format(_T("%Y-%m-%d"));
	
	for(int i = 0; i < m_tabBodyCheck.GetItemCount(); i++)
	{
		TCITEM tcItem = {0};
		tcItem.mask = TCIF_PARAM;
		m_tabBodyCheck.GetItem(i, &tcItem);
		
		CScrollPage* pPage = (CScrollPage*)tcItem.lParam;
		if(pPage)
			pPage->SetLastColTitile(strToday, 0);
	}

	return TRUE;
}
